package com.demo.chapter5stream2;

import java.util.OptionalInt;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import com.demo.domain.Dish;

public class MapTo {

	public static void demo(){
		Dish.menu.stream().mapToInt(Dish::getCalories).sum();

		//把IntStream转回Stream<Integer>
		Stream<Integer> l = Dish.menu.stream().mapToInt(Dish::getCalories).boxed();

		//获取最大值
		OptionalInt maxo = Dish.menu.stream().mapToInt(Dish::getCalories).max();
		int max = maxo.orElse(1);

		IntStream.rangeClosed(1, 100).filter(d -> d % 2 == 0);
	}

	//生成勾股数的流
	public static void demo2(){
		//t[2]%1 == 0表示没有小数部分
		Stream<double[]> s = IntStream.rangeClosed(1, 100).boxed()
			.flatMap(a -> IntStream.rangeClosed(a, 100).mapToObj(b -> new double[]{a, b, Math.sqrt(a * a + b * b)}).filter(t -> t[2]%1 == 0));
		s.forEach(arr -> System.out.println(arr[0] + "," + arr[1] + "," + arr[2]));
	}

	public static void main(String[] args) {
		demo2();
	}

}
